#include "fbo.h"

#include <gl\glew.h>
#include <gl\glfw.h>

#include <cstdio>

FBO::FBO()
{


}

FBO::~FBO()
{
  glDeleteFramebuffers(1, &fb);
  glDeleteRenderbuffers(1, &color_rb);
  glDeleteBuffers(1, &fb);
}


void FBO::init()
{
	// FBO's are a pixel-buffer aka p-buffer replacement, check out this for example use
	// FBO's dont actually store pixeldata, they store state. Like VBO's dont store vertices.
	// http://www.opengl.org/wiki/GL_EXT_framebuffer_object#Color_only

	/* you can attach various types of data to a FBO, like
		-color 
		-depth
	*/
	
  // generate namespace for the frame buffer, colorbuffer and depthbuffer
  glGenFramebuffers(1, &fb);
  glBindFramebuffer(GL_FRAMEBUFFER, fb);

  glGenRenderbuffers(1, &color_rb);
  glBindRenderbuffer(GL_RENDERBUFFER, color_rb);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 512, 512);
  // Attach color buffer to FBO
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_RENDERBUFFER, color_rb);
	
  GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  if(status==GL_FRAMEBUFFER_COMPLETE) 
	  fprintf(stdout, "Yay!! GL_FRAMEBUFFER_COMPLETE! \n");
  else{
	  fprintf(stdout, "framebuffer config not supported :( ");
	  //exit(1);
  }

  // Go back to regular frame buffer rendering
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void FBO::begin()
{
	glBindFramebuffer(GL_FRAMEBUFFER, fb);
}


void FBO::end()
{
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
}